﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL
{
    public class ProductTypeRepo
    {
        public IList<ProductType> GetProductTypes()
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.ProductType.Where(x => x.IsDeleted == false).OrderBy(x =>x.Name).ToList();
            }
        }

        public void AddProductType(ProductType productType)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                productType.IsDeleted = false;
                dbContext.ProductType.Add(productType);
                dbContext.SaveChanges();
            }
        }

        public void UpdateProductType(ProductType productType)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                productType.IsDeleted = false;
                dbContext.Entry(productType).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
            }
        }

        public bool DeleteProductType(int producTypeId)
        {
            bool canDeleted = true;
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                var product = dbContext.Product.Where(x => x.ProductTypeId == producTypeId).ToList();
                if (product.Count > 0)
                {
                    canDeleted = false;
                }
                else
                {
                    int returnValue = dbContext.DeleteProductType(producTypeId);
                    if (returnValue == 0)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                return canDeleted;
            }
        }

       
    }
}
